package com.gc.cscott.verify.contest2012jan;

import com.gc.cscott.verify.JavaTaskOutputVerifier;
import com.gc.cscott.verify.OutputVerifier.VerifiedOutput;
import java.util.Arrays;

/**
 *
 * @author User
 */
public class CheckZ2Table  implements JavaTaskOutputVerifier  {

    public VerifiedOutput verify(String inputText, String outputText, String validationHintText) {
        if (outputText.trim().length()!=validationHintText.trim().length()){
            return VerifiedOutput.outputIsWrong("Wrong BG nubmer.");
        } else {
            char[] hintRaw = validationHintText.trim().toCharArray();
            Arrays.sort(hintRaw);
            char[] outputRaw = outputText.trim().toCharArray();
            Arrays.sort(outputRaw);
            if (!Arrays.equals(hintRaw, outputRaw)){
                return VerifiedOutput.outputIsWrong("Output is not matching to input");
            } else {
                final int competitorOutput = collaborativeEffect(outputText);
                final int correctOutout = collaborativeEffect(validationHintText);
                if (competitorOutput<competitorOutput){
                    return VerifiedOutput.outputIsWrong("Passed collobortive "+competitorOutput+", correct "+correctOutout);
                } else if (competitorOutput==correctOutout){
                    return VerifiedOutput.outputIsCorrect();
                } else {
                    return VerifiedOutput.outputIsWrong("Passed is better!!! "+competitorOutput+", 'correct' "+correctOutout);
                }
            }
        }
    }

    private int collaborativeEffect(String table){
        final String lowerTrim = table.trim().toLowerCase();

        final String work = lowerTrim+lowerTrim.substring(0,2);
        int effect = 0;
        for(int i=0;i<work.length()-2;i++){
            final String triad = work.substring(i,i+3);
            if ("gbg".equals(triad)||"ggb".equals(triad)||"bgg".equals(triad)){
                effect+=1;
            }
        }
        return effect;
    }

    public static void main(String[] args) {
        for(final String test:new String[]{
            "BGB",
            "GBG",
            "BGBG",
            "GBGB",
            "BBGG",
            "GGBB"
        }){
            System.out.println(test+":"+new CheckZ2Table().collaborativeEffect(test));
        }
    }
}
